package com.zky.user.controller;

import com.zky.basic.dto.AccountDto;
import com.zky.basic.exception.BusinessException;
import com.zky.user.domain.Logininfo;
import com.zky.user.service.ILogininfoService;
import com.zky.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 后端接口类；登录
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;
    @PostMapping("/account")
    public AjaxResult loginAccount(@RequestBody AccountDto accountDto){
        try {
            Map<String, Object>map = logininfoService.loginAccount(accountDto);
            return new AjaxResult(true,map);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult loginWechat(@RequestBody Map<String , String> map){
        try {

            return logininfoService.loginWechat(map);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String , String> map){
        try {

            return logininfoService.wechatBinder(map);

        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!!");
        }
    }

}
